package com.simdo.g768.Dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Spinner;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.simdo.g768.Adapter.CellManageAdapter;
import com.simdo.g768.Bean.CellManageBean;
import com.simdo.g768.MainActivity;
import com.simdo.g768.R;
import com.simdo.g768.Util.GsonUtil;
import com.simdo.g768.Util.PrefUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CellManageDialog extends Dialog {

    private final Context mContext;

    public CellManageDialog(Context context) {
        super(context, R.style.Theme_B97621);
        this.mContext = context;

        View view = View.inflate(context, R.layout.dialog_cell_manage, null);
        this.setContentView(view);
        this.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
        //this.setCancelable(false);   // 返回键不消失
        //设置dialog大小，这里是一个小赠送，模块好的控件大小设置
        Window window = this.getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        window.setNavigationBarColor(Color.parseColor("#2A72FF"));
        //window.setGravity(Gravity.BOTTOM); //可设置dialog的位置
        window.getDecorView().setPadding(0, 0, 0, 0); //消除边距
        //StatusBarUtil.setLightStatusBar(window, true);
        WindowManager.LayoutParams lp = window.getAttributes();

        lp.width = WindowManager.LayoutParams.MATCH_PARENT;   //设置宽度充满屏幕
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(lp);

        initView(view);
    }

    private void initView(View root) {
        root.findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        
        RecyclerView rv_list = root.findViewById(R.id.rv_list);

        List<CellManageBean> list = new ArrayList<>();
        for (Map.Entry<Integer, List<String>> entry : MainActivity.getInstance().cellMap.entrySet()) {
            CellManageBean bean = new CellManageBean(entry.getKey(), "TX" + (entry.getKey() + 1));
            bean.setList(entry.getValue());
            list.add(bean);
        }
        CellManageAdapter adapter = new CellManageAdapter(mContext, list);
        rv_list.setLayoutManager(new LinearLayoutManager(mContext));
        rv_list.setAdapter(adapter);

        root.findViewById(R.id.iv_add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Dialog dialog = MainActivity.getInstance().createDialog(false);

                View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_add_cell_band, null);
                final Spinner sp_tx = view.findViewById(R.id.sp_tx);
                final EditText ed_band = view.findViewById(R.id.ed_band);
                view.findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //按下确定键后的事件
                        int position = sp_tx.getSelectedItemPosition();
                        String band = ed_band.getText().toString();
                        if (band.isEmpty()){
                            MainActivity.getInstance().showToast(mContext.getString(R.string.not_empty));
                            return;
                        }
                        band = band.toUpperCase();
                        String substring = band.substring(0, 1);
                        if (!substring.equals("B") && !substring.equals("N")){
                            MainActivity.getInstance().showToast(mContext.getString(R.string.n_b_title));
                            return;
                        }
                        for (List<String> list : MainActivity.getInstance().cellMap.values()) {
                            if (list.contains(band)){
                                MainActivity.getInstance().showToast(mContext.getString(R.string.value_exist));
                                return;
                            }
                        }

                        List<String> bandList = MainActivity.getInstance().cellMap.get(position);
                        bandList.add(band);
                        for (CellManageBean bean : list) {
                            if (bean.getCellId() == position){
                                bean.setList(bandList);
                                adapter.notifyItemChanged(position);
                                break;
                            }
                        }
                        PrefUtil.build().putString("cell_manage", GsonUtil.listMapToJson(MainActivity.getInstance().cellMap));
                        MainActivity.getInstance().showToast(mContext.getString(R.string.update_ok));
                        MainActivity.getInstance().closeDialog(dialog);
                    }
                });
                view.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        MainActivity.getInstance().closeDialog(dialog);
                    }
                });
                MainActivity.getInstance().showDialog(dialog, view, false);
            }
        });

    }
}
